class ActiveClients {
    private activeClients: Map<string, number> = new Map();
    private TIMEOUT = 1000 * 60 * 5; // 5分钟
    private CLEANUP_INTERVAL = 1000 * 60; // 每分钟清理一次

    constructor() {
        // 启动清理定时器
        setInterval(() => {
            const now = Date.now();
            for (const [ip, lastActive] of this.activeClients.entries()) {
                if (now - lastActive > this.TIMEOUT) {
                    console.log(`%c客户端 ${ip} 被认为离开 (超时).`, 'color: red;');
                    this.activeClients.delete(ip);
                }
            }
        }, this.CLEANUP_INTERVAL);
    }

    addClient(ip: string) {
        this.activeClients.set(ip, Date.now());
    }

    getActiveClients(ip: string) {
        return Array.from(this.activeClients.keys()).filter(key => key !== ip);
    }
}  

export default new ActiveClients();